package cn.com.baozun.sany.websocket;

import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.security.SecureRandom;
import java.util.Random;

/**
 * @title: MessageSend
 * @description:
 * @author: guan.xin
 * @create: 2021-05-25 17:19
 * @since: JDK8
 */
@Service
public class MessageSendService {

    private WebSocketSession session;

    public void setSession(WebSocketSession session) {
        this.session = session;
    }

    public void send() throws InterruptedException, IOException {
        int i = 50;

        while (i > 0) {
            Random random = new SecureRandom();
            session.sendMessage(new TextMessage(String.format("数据发生变化，当前人数：%s", random.nextInt(100))));
            Thread.sleep(2000);
            i--;
        }
    }
}
